package com.shire.shire_interface.controller;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.utils.StrUtils;
import com.shire.shire_interface.conf.InstanceManager;
import com.shire.shire_interface.model.BasePageExample;
import com.shire.shire_interface.model.account.WxStation;
import com.shire.shire_interface.model.user.SystemUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

public class BaseController {

    public Map<String, Object> responseSuccess() {
        return responseSuccess("操作成功");
    }

    public Map<String, Object> responseSuccess(String msg) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("success", true);
        result.put("msg", msg);
        result.put("status", 0);
        return result;
    }

    public Map<String, Object> responseSuccess(Integer status, String msg) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("success", true);
        result.put("msg", msg);
        result.put("status", status);
        return result;
    }

    public Map<String, Object> responseFailure() {
        return responseFailure("操作失败");
    }

    public Map<String, Object> responseFailure(String msg){
        return responseFailure(-1, msg);
    }

    public Map<String, Object> responseFailure(Integer status, String msg) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("success", false);
        result.put("msg", msg);
        result.put("status", status);
        return result;
    }

    protected String getK(HttpServletRequest request) {
        return (String)request.getAttribute("k");
    }

    protected String getN(HttpServletRequest request) {
        String n = (String)request.getAttribute("n");
        return n == null ? "99" : n;
    }

    protected WxStation getCurrentStation(HttpServletRequest request){
        return InstanceManager.getInstance().getStationService().getStationByNK(getN(request), getK(request));
    }


    public int getIntValue(HttpServletRequest request, String name, int def) {
        return StrUtils.getIntValue(request.getParameter(name), def);
    }

    public Long getLongValue(HttpServletRequest request, String name, long def) {
        return StrUtils.getLongValue(request.getParameter(name), def);
    }

    public String getStringValue(HttpServletRequest request, String name, String def) {
        return StrUtils.getNullStr(request.getParameter(name), def);
    }

    public int getPageStartRow(int pageNo, int pageSize ){
        return pageSize * (pageNo - 1);
    }

    public boolean checkLoginUser(HttpSession session){
        if (session.getAttribute("user")==null){
            return false;
        }
        return true;
    }

    public boolean checkLoginUserByCsId(HttpSession session, Integer csId){
//        if (session.getAttribute("user") == null){
//            return false;
//        }
//
//        SystemUser user = (SystemUser) session.getAttribute("user");
//        if (user.getId()!=csId){
//            return false;
//        }
        return true;
    }
    public void addPageParamToExample(JSONObject jsonObject, BasePageExample example) {
        if (jsonObject.containsKey("pageSize") && jsonObject.containsKey("pageNo")){
            Integer pageNo = jsonObject.getInteger("pageNo");
            Integer pageSize = jsonObject.getInteger("pageSize");
            example.setLimitStart(getPageStartRow(pageNo, pageSize));
            example.setLimitSize(pageSize);
        }
    }

    public SystemUser getCurrentUser(HttpSession session){
        if (session.getAttribute("user") == null){
            return null;
        }

        return  (SystemUser) session.getAttribute("user");

    }
}
